// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using System.Text;
using System.Text.RegularExpressions;
using Mono.Unix;
using NLog;

namespace wxGadu
{	
	/// <summary>
	/// Packs message body into HTML 
	/// so it can be displayed into 
	/// <c>ChatFrame.TopChatFramePanel.messageWindow</c>
	/// </summary>
	public class MessageFormatter
	{
//		private enum FontStyle
//		{
//			normal,
//			italic
//		}
//		
//		private enum FontWeight
//		{
//			normal,
//			bold
//		}
		
		#region regex objects
//		public Regex fontSizeRegex = new Regex(PatternVariables.FONT_SIZE);
//		public  Regex fontStyleRegex = new Regex(PatternVariables.FONT_STYLE);
//		public  Regex fontWeightRegex = new Regex(PatternVariables.FONT_WEIGHT);
//		public  Regex fontFaceRegex = new Regex(PatternVariables.FONT_FACE);
		private Regex senderIdRegex = new Regex(PatternVariables.SENDER_ID);
		private Regex dateRegex = new Regex(PatternVariables.DATE);
		private Regex messageBodyRegex = new Regex(PatternVariables.MESSAGE_BODY);
		#endregion
		
		#region data members
		private string messagePattern;
//		private string fontSize;
//		private string fontFace;
//		private MessageFormatter.FontStyle fontStyle;
//		private MessageFormatter.FontWeight fontWeigth;
		#endregion
				
		/// <summary>
		/// Ctor
		/// </summary>
		/// <param name="msgPattern">
		/// A <see cref="System.String"/>
		/// message formatting pattern
		/// </param>
		public MessageFormatter(string msgPattern)
		{
			messagePattern = msgPattern;
						
			
//			System.Console.WriteLine("font face: {0}", fontFaceRegex.Replace(messagePattern, fontFace));
//			messagePattern = fontFaceRegex.Replace(messagePattern, fontFace);
//			messagePattern = fontStyleRegex.Replace(messagePattern, fontStyle.ToString());
//			messagePattern = fontWeightRegex.Replace(messagePattern, fontWeigth.ToString());
//			messagePattern = fontSizeRegex.Replace(messagePattern, fontSize);
//			System.Console.WriteLine("font size: {0}", fontSizeRegex.Replace(messagePattern, fontSize));
		}
		
		/// <summary>
		/// Format message for chat window
		/// </summary>
		/// <param name="message">
		/// A <see cref="ChatMessage"/>
		/// chat message object with raw message body
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// formatted message body 
		/// </returns>
		public string FormatMessage(ChatMessage message)
		{
			string formattedMessage = messagePattern;
			
			formattedMessage = senderIdRegex.Replace(formattedMessage, message.Name == string.Empty ? message.Uin.ToString() : message.Name);
			formattedMessage = dateRegex.Replace(formattedMessage, message.Time.ToString("G"));
			formattedMessage = messageBodyRegex.Replace(formattedMessage, message.Message);

			
			return formattedMessage;
		}
		
		/// <summary>
		/// Format message for history browser
		/// </summary>
		/// <param name="message">
		/// A <see cref="HistoryMsg"/>
		/// </param>
		/// <returns>
		/// A <see cref="System.String"/>
		/// </returns>
		public string FormatMessage(HistoryMsg message)
		{
			string formattedMessage = messagePattern;
			
			formattedMessage = senderIdRegex.Replace(formattedMessage, message.N == string.Empty ? message.U.ToString() : message.N);
			formattedMessage = dateRegex.Replace(formattedMessage, message.D);
			formattedMessage = messageBodyRegex.Replace(formattedMessage, message.M);
			
			return formattedMessage;
		}
	}
}
